package cn.edu.ncut.cs.springboot.springbootdatabase.service;

import cn.edu.ncut.cs.springboot.springbootdatabase.entity.User;
import cn.edu.ncut.cs.springboot.springbootdatabase.repository.UserRepository;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    private final UserRepository userRepository;

    public CustomUserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库中查找用户
        User userEntity = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found: " + username));

        // 将用户实体转换为 Spring Security 的 UserDetails
        return org.springframework.security.core.userdetails.User.builder()
                .username(userEntity.getUsername())
                .password(userEntity.getPassword()) // 明文密码
                .authorities(userEntity.getRoles().stream()
                        .map(role -> new SimpleGrantedAuthority("ROLE_" + role.getName())) // 转换角色为权限
                        .collect(Collectors.toList()))
                .accountLocked(!userEntity.isEnabled()) // 根据是否启用锁定账户
                .build();
    }
}




//package cn.edu.ncut.cs.springboot.springbootdatabase.service;
//
//import cn.edu.ncut.cs.springboot.springbootdatabase.entity.User;
//import cn.edu.ncut.cs.springboot.springbootdatabase.repository.UserRepository;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//
//import java.util.stream.Collectors;
//
//@Service
//public class CustomUserDetailsService implements UserDetailsService {
//
//    private final UserRepository userRepository;
//
//    public CustomUserDetailsService(UserRepository userRepository) {
//        this.userRepository = userRepository;
//    }
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        // 从数据库中查找用户
//        User userEntity = userRepository.findByUsername(username)
//                .orElseThrow(() -> new UsernameNotFoundException("User not found: " + username));
//
//        // 将用户实体转换为 Spring Security 的 UserDetails
//        return org.springframework.security.core.userdetails.User.builder()
//                .username(userEntity.getUsername())
//                .password(userEntity.getPassword()) // 明文密码
//                .authorities(userEntity.getRoles().stream()
//                        .map(role -> new SimpleGrantedAuthority("ROLE_" + role.getName())) // 转换角色为权限
//                        .collect(Collectors.toList()))
//                .accountLocked(!userEntity.isEnabled()) // 根据是否启用锁定账户
//                .build();
//    }
//}
